package cn.com.xxlibrary.action;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import cn.com.xxlibrary.orm.ManagerInfoBean;
import cn.com.xxlibrary.service.IBorrowService;
import cn.com.xxlibrary.service.ISystemService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@ParentPackage("myStruts")
@Namespace(value = "/")
public class BorrowAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int pageNo = 0, pageSize = 10;
	private String wd;
	private String op;

	private int uid, bid;

	private ManagerInfoBean manager;
	private int borCount;

	private String from;

	@Resource
	private ISystemService systemService;

	@Resource
	private IBorrowService borrowService;

	@Action(value = "borrowBook", results = {
			@Result(name = SUCCESS, location = "/listBook", type = "redirect"),
			@Result(name = "enter", location = "/books/bookBorrow.jsp") })
	public String borrowBook() {

		if (uid == 0) {
			ActionContext.getContext().getSession()
					.put("SESSION_LISTMANAGER", borrowService.selManager());
			return "enter";
		} else {

			borrowService.borrowBook(bid, uid);
			return SUCCESS;
		}
	}

	@Action(value = "returnBook", results = {
			@Result(name = "list", location = "/listBook", type = "redirect"),
			@Result(name = "search", location = "/listBook", type = "redirect"),
			@Result(name = "loan", location = "/bookOnLoan", type = "redirect") })
	public String returnBook() {
		borrowService.returnBook(bid);
		return from;
	}

	@Action(value = "showMe", results = { @Result(name = SUCCESS, location = "/manager/managerInfo.jsp") })
	public String showMe() {
		manager = (ManagerInfoBean) ActionContext.getContext().getSession()
				.get("SESSION_MANAGER_LOGIN");
		borCount = (int) borrowService.showMe(manager).get("borCount");

		return SUCCESS;
	}

	@Action(value = "myBorrow", results = { @Result(name = SUCCESS, location = "/books/myBorrowList.jsp") })
	public String myBorrow() {
		manager = (ManagerInfoBean) ActionContext.getContext().getSession()
				.get("SESSION_MANAGER_LOGIN");

		int start = (pageNo - 1) * pageSize;
		Map map = borrowService.myBorrow(start, pageSize, manager);

		ActionContext.getContext().getSession()
				.put("SESSION_MY_LIST", map.get("queryResult"));
		ActionContext.getContext().getSession()
				.put("SESSION_MY_PAGESIZE", pageSize);
		ActionContext.getContext().getSession()
				.put("SESSION_MY_PAGENO", pageNo);
		ActionContext.getContext().getSession()
				.put("SESSION_MY_COUNT", map.get("count"));

		return SUCCESS;
	}

	@Action(value = "bookOnLoan", results = { @Result(name = SUCCESS, location = "/books/booksOnLoan.jsp") })
	public String bookOnLoan() {
		int start = (pageNo - 1) * pageSize;
		Map map = borrowService.bookOnLoan(bid,start, pageSize, wd);

		ActionContext.getContext().getSession()
				.put("SESSION_LOAN_LIST", map.get("queryResult"));
		ActionContext.getContext().getSession()
				.put("SESSION_LOAN_PAGESIZE", pageSize);
		ActionContext.getContext().getSession()
				.put("SESSION_LOAN_PAGENO", pageNo);
		ActionContext.getContext().getSession()
				.put("SESSION_LOAN_COUNT", map.get("count"));
		bid=0;
		return SUCCESS;
	}

	// ////////////////////////////////////////////////

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getWd() {
		return wd;
	}

	public void setWd(String wd) {
		this.wd = wd;
	}

	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	public int getBid() {
		return bid;
	}

	public void setBid(int bid) {
		this.bid = bid;
	}

	public ManagerInfoBean getManager() {
		return manager;
	}

	public void setManager(ManagerInfoBean manager) {
		this.manager = manager;
	}

	public ISystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(ISystemService systemService) {
		this.systemService = systemService;
	}

	public IBorrowService getBorrowService() {
		return borrowService;
	}

	public void setBorrowService(IBorrowService borrowService) {
		this.borrowService = borrowService;
	}

	public void setBorCount(int borCount) {
		this.borCount = borCount;
	}

	public int getBorCount() {
		return borCount;
	}

	public String getOp() {
		return op;
	}

	public void setOp(String op) {
		this.op = op;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

}
